package com.neusoft.neuiotms.dcvm.service.impl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.neuiotms.dcvm.service.CameraService;

@Service
public class CameraServiceImpl implements CameraService {

    @Autowired
    private RestTemplate restTemplate;
    @Value("${cmdb.proxy.target-url}")
    private String cmdbUrl;
    @Value("${cmdb.camera.schema}")
    private String cameraSchema;

    @SuppressWarnings("unchecked")
    @Override
    public PageResultDTO<Map<String, Object>> search(HttpServletRequest request) {
        final PageResultDTO<Map<String, Object>> result =
                this.restTemplate.getForObject(this.getCameraInstanceUrl() + "?" + request.getQueryString(),
                        PageResultDTO.class);
        return result;
    }

    private String getCameraInstanceUrl() {
        final StringBuilder builder = new StringBuilder();
        builder.append(this.cmdbUrl).append("/").append("instances")
                .append("/").append("schema")
                .append("/").append(this.cameraSchema)
                .append("/").append("page");
        return builder.toString();
    }

}
